<?php

namespace App\Models;

use App\Models\Scopes\CreateAtDesc;
use Carbon\Carbon;
use App\Models\BaseModel;
use App\Helpers\MoneyHelper;
use App\Format\BaseFormat;

/**
 * App\Models\Area
 *
 * @property int $id
 * @property string $name 地区名称
 * @property int $pid 上级id
 * @property int $level 深度
 * @property mixed $created_at
 * @property mixed $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Area apiBaseQuery($whereParams = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Area bgBaseQuery($whereParams = [])
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel deleteNot()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel enableYes()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel findById(bool $id, bool $throw = true)
 * @method static \Illuminate\Database\Eloquent\Builder|Area newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Area newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Area query()
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereIds($ids = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereLike($key, $value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area wherePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property string $name_first 拼音首字母
 * @property string $city_no 城市编号，例如：北京(BJ)
 * @property int $region_id 地区ID
 * @property int $province_id 城市id
 * @property int $city_id 区域id
 * @property int $sort 排序
 * @property int $is_hot 是否热门：1是 0否
 * @property int $is_enable 启用状态，1启用，0禁用
 * @property string $month1_price 1个月VIP价格
 * @property string $month3_price 3个月VIP价格
 * @property string $month6_price 6个月VIP价格
 * @property string $month12_price 12个月VIP价格
 * @property int $is_sys 是否为系统插入：1是 0否
 * @property int $is_delete 是否删除：1是 0否
 * @property-read mixed $check_fstatus
 * @property-read mixed $faddress
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel bgWhereDateTimeRange($field, $date_range = null)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel checkNo()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel checkYes()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel createdAtRangeQuery($params = [])
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel notCheck()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel orWhereLike($key, $value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel paramsQuery($fields, $params = [])
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel settleYes()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel waitingCheckQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereCityNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereDateTimeRange($field, $start_at = null, $end_at = null)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereIsDelete($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereIsEnable($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereIsHot($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereIsSys($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereMonth12Price($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereMonth1Price($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereMonth3Price($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereMonth6Price($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereNameFirst($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereProvinceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereRegionId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Area whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereValueRange($field, $min_value, $max_value)
 */
class Area extends BaseModel
{
    const AREA_LEVE_PROVINCE = 1;
    const AREA_LEVE_CITY     = 2;
    const AREA_LEVE_DISTRICT = 3;

    /**
     * Define table name
     *
     * @var string
     */
    protected $table = 'areas';

    /**
     * Define a primary key
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * A property that cannot be batched.
     *
     * @var array
     */
    protected $guarded = ['id'];
    protected $hidden  = ['updated_at'];
    /*********************************************属性格式化***************************************************************/

    /*********************************************设置属性***************************************************************/

    /*********************************************模型操作***************************************************************/
    public function scopeBgBaseQuery($query, $whereParams = [])
    {
    }

    public function scopeApiBaseQuery($query, $whereParams = [])
    {
    }

    public static function getNameById($id)
    {
        return self::find($id)->name ?? "";
    }

    public static function getIdByName($name)
    {
        return self::whereName($name)->first()->id ?? 0;
    }

    public static function getProvinces()
    {
        return self::whereLevel(self::AREA_LEVE_PROVINCE)->get();
    }

    public static function getNames($ids = [])
    {
        $list = self::whereIds($ids)->get();
        return set_array_by_keys($list, 'id');
    }

    public static function getRegionProvinces($region_id)
    {
        return self::withoutGlobalScope(new CreateAtDesc())->whereRegionId($region_id)->select(['id as value', 'name as label'])->get();
    }

    public static function getChildren($pid = 0)
    {
        return self::wherePid($pid)->select(['id as value', 'name as label'])->get();
    }

    public static function getTree()
    {
        $areas = Area::where('level', '<=', 3)->get();
        $areas = array_to_tree(json_decode(json_encode($areas), true));
        return $areas;
    }
    /*********************************************模型关联***************************************************************/

}
